/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.utils;

import org.toedo.model.enums.WikiMarkupType;

/**
 * This class holds all the wiki formatting patterns.
 *
 */
public class WikiUtils {
	
	/* Textile constants */
	private static final String TEXTILE_BOLD_PATTERN = "*%s*"; //$NON-NLS-1$
	private static final String TEXTILE_ITALIC_PATTERN = "_%s_"; //$NON-NLS-1$
	private static final String TEXTILE_H1_PATTERN = "h1. %s"; //$NON-NLS-1$
	private static final String TEXTILE_H2_PATTERN = "h2. %s"; //$NON-NLS-1$
	private static final String TEXTILE_H3_PATTERN = "h3. %s"; //$NON-NLS-1$
	private static final String TEXTILE_H4_PATTERN = "h4. %s"; //$NON-NLS-1$
	private static final String TEXTILE_LINK_PATTERN = "\"%s\":%s"; //$NON-NLS-1$
	
	private static final int TEXTILE_BOLD_DELTA = 1;
	private static final int TEXTILE_ITALIC_DELTA = 1;
	private static final int TEXTILE_H1_DELTA = 0;
	private static final int TEXTILE_H2_DELTA = 0;
	private static final int TEXTILE_H3_DELTA = 0;
	private static final int TEXTILE_H4_DELTA = 0;
	
	/* MediaWiki constants */
	private static final String MEDIAWIKI_BOLD_PATTERN = "'''%s'''"; //$NON-NLS-1$
	private static final String MEDIAWIKI_ITALIC_PATTERN = "''%s''"; //$NON-NLS-1$
	private static final String MEDIAWIKI_H1_PATTERN = "=%s="; //$NON-NLS-1$
	private static final String MEDIAWIKI_H2_PATTERN = "==%s=="; //$NON-NLS-1$
	private static final String MEDIAWIKI_H3_PATTERN = "===%s==="; //$NON-NLS-1$
	private static final String MEDIAWIKI_H4_PATTERN = "====%s===="; //$NON-NLS-1$
	private static final String MEDIAWIKI_LINK_PATTERN = "[%s %s]"; //$NON-NLS-1$
	
	private static final int MEDIAWIKI_BOLD_DELTA = 3;
	private static final int MEDIAWIKI_ITALIC_DELTA = 2;
	private static final int MEDIAWIKI_H1_DELTA = 1;
	private static final int MEDIAWIKI_H2_DELTA = 2;
	private static final int MEDIAWIKI_H3_DELTA = 3;
	private static final int MEDIAWIKI_H4_DELTA = 4;
	
	/* Confluence constants */
	private static final String CONFLUENCE_BOLD_PATTERN = "*%s*"; //$NON-NLS-1$
	private static final String CONFLUENCE_ITALIC_PATTERN = "_%s_"; //$NON-NLS-1$
	private static final String CONFLUENCE_H1_PATTERN = "h1. %s"; //$NON-NLS-1$
	private static final String CONFLUENCE_H2_PATTERN = "h2. %s"; //$NON-NLS-1$
	private static final String CONFLUENCE_H3_PATTERN = "h3. %s"; //$NON-NLS-1$
	private static final String CONFLUENCE_H4_PATTERN = "h4. %s"; //$NON-NLS-1$
	private static final String CONFLUENCE_LINK_PATTERN = "[%s|%s]"; //$NON-NLS-1$
	
	private static final int CONFLUENCE_BOLD_DELTA = 1;
	private static final int CONFLUENCE_ITALIC_DELTA = 1;
	private static final int CONFLUENCE_H1_DELTA = 0;
	private static final int CONFLUENCE_H2_DELTA = 0;
	private static final int CONFLUENCE_H3_DELTA = 0;
	private static final int CONFLUENCE_H4_DELTA = 0;
	
	/* Trac constants */
	private static final String TRAC_BOLD_PATTERN = "'''%s'''"; //$NON-NLS-1$
	private static final String TRAC_ITALIC_PATTERN = "''%s''"; //$NON-NLS-1$
	private static final String TRAC_H1_PATTERN = "=%s="; //$NON-NLS-1$
	private static final String TRAC_H2_PATTERN = "==%s=="; //$NON-NLS-1$
	private static final String TRAC_H3_PATTERN = "===%s==="; //$NON-NLS-1$
	private static final String TRAC_H4_PATTERN = "====%s===="; //$NON-NLS-1$
	private static final String TRAC_LINK_PATTERN = "[%s %s]"; //$NON-NLS-1$
	
	private static final int TRAC_BOLD_DELTA = 3;
	private static final int TRAC_ITALIC_DELTA = 2;
	private static final int TRAC_H1_DELTA = 1;
	private static final int TRAC_H2_DELTA = 2;
	private static final int TRAC_H3_DELTA = 3;
	private static final int TRAC_H4_DELTA = 4;
	
	public static String getBoldPattern(WikiMarkupType markupType) {
		switch (markupType) {
		case EnTextile : return TEXTILE_BOLD_PATTERN;
		case EnMediawiki : return MEDIAWIKI_BOLD_PATTERN;
		case EnConfluence : return CONFLUENCE_BOLD_PATTERN;
		case EnTrac : return TRAC_BOLD_PATTERN;
		default : return TEXTILE_BOLD_PATTERN;
		}
	}
	
	public static String getItalicPattern(WikiMarkupType markupType) {
		switch (markupType) {
		case EnTextile : return TEXTILE_ITALIC_PATTERN;
		case EnMediawiki : return MEDIAWIKI_ITALIC_PATTERN;
		case EnConfluence : return CONFLUENCE_ITALIC_PATTERN;
		case EnTrac : return TRAC_ITALIC_PATTERN;
		default : return TEXTILE_ITALIC_PATTERN;
		}
	}
	
	public static String getH1Pattern(WikiMarkupType markupType) {
		switch (markupType) {
		case EnTextile : return TEXTILE_H1_PATTERN;
		case EnMediawiki : return MEDIAWIKI_H1_PATTERN;
		case EnConfluence : return CONFLUENCE_H1_PATTERN;
		case EnTrac : return TRAC_H1_PATTERN;
		default : return TEXTILE_H1_PATTERN;
		}
	}
	
	public static String getH2Pattern(WikiMarkupType markupType) {
		switch (markupType) {
		case EnTextile : return TEXTILE_H2_PATTERN;
		case EnMediawiki : return MEDIAWIKI_H2_PATTERN;
		case EnConfluence : return CONFLUENCE_H2_PATTERN;
		case EnTrac : return TRAC_H2_PATTERN;
		default : return TEXTILE_H2_PATTERN;
		}
	}
	
	public static String getH3Pattern(WikiMarkupType markupType) {
		switch (markupType) {
		case EnTextile : return TEXTILE_H3_PATTERN;
		case EnMediawiki : return MEDIAWIKI_H3_PATTERN;
		case EnConfluence : return CONFLUENCE_H3_PATTERN;
		case EnTrac : return TRAC_H3_PATTERN;
		default : return TEXTILE_H3_PATTERN;
		}
	}
	
	public static String getH4Pattern(WikiMarkupType markupType) {
		switch (markupType) {
		case EnTextile : return TEXTILE_H4_PATTERN;
		case EnMediawiki : return MEDIAWIKI_H4_PATTERN;
		case EnConfluence : return CONFLUENCE_H4_PATTERN;
		case EnTrac : return TRAC_H4_PATTERN;
		default : return TEXTILE_H4_PATTERN;
		}
	}
	
	public static String getFormatedLink(WikiMarkupType markupType, String title, String url) {
		switch (markupType) {
		case EnTextile : return String.format(TEXTILE_LINK_PATTERN, title, url);
		case EnMediawiki : return String.format(MEDIAWIKI_LINK_PATTERN, url, title);
		case EnConfluence : return String.format(CONFLUENCE_LINK_PATTERN, title, url);
		case EnTrac : return String.format(TRAC_LINK_PATTERN, url, title);
		default : return String.format(TEXTILE_LINK_PATTERN, title, url);
		}
	}
	
	public static int getBoldDelta(WikiMarkupType markupType) {
		switch (markupType) {
		case EnTextile : return TEXTILE_BOLD_DELTA;
		case EnMediawiki : return MEDIAWIKI_BOLD_DELTA;
		case EnConfluence : return CONFLUENCE_BOLD_DELTA;
		case EnTrac : return TRAC_BOLD_DELTA;
		default : return TEXTILE_BOLD_DELTA;
		}
	}
	
	public static int getItalicDelta(WikiMarkupType markupType) {
		switch (markupType) {
		case EnTextile : return TEXTILE_ITALIC_DELTA;
		case EnMediawiki : return MEDIAWIKI_ITALIC_DELTA;
		case EnConfluence : return CONFLUENCE_ITALIC_DELTA;
		case EnTrac : return TRAC_ITALIC_DELTA;
		default : return TEXTILE_ITALIC_DELTA;
		}
	}
	
	public static int getH1Delta(WikiMarkupType markupType) {
		switch (markupType) {
		case EnTextile : return TEXTILE_H1_DELTA;
		case EnMediawiki : return MEDIAWIKI_H1_DELTA;
		case EnConfluence : return CONFLUENCE_H1_DELTA;
		case EnTrac : return TRAC_H1_DELTA;
		default : return TEXTILE_H1_DELTA;
		}
	}
	
	public static int getH2Delta(WikiMarkupType markupType) {
		switch (markupType) {
		case EnTextile : return TEXTILE_H2_DELTA;
		case EnMediawiki : return MEDIAWIKI_H2_DELTA;
		case EnConfluence : return CONFLUENCE_H2_DELTA;
		case EnTrac : return TRAC_H2_DELTA;
		default : return TEXTILE_H2_DELTA;
		}
	}
	
	public static int getH3Delta(WikiMarkupType markupType) {
		switch (markupType) {
		case EnTextile : return TEXTILE_H3_DELTA;
		case EnMediawiki : return MEDIAWIKI_H3_DELTA;
		case EnConfluence : return CONFLUENCE_H3_DELTA;
		case EnTrac : return TRAC_H3_DELTA;
		default : return TEXTILE_H3_DELTA;
		}
	}
	
	public static int getH4Delta(WikiMarkupType markupType) {
		switch (markupType) {
		case EnTextile : return TEXTILE_H4_DELTA;
		case EnMediawiki : return MEDIAWIKI_H4_DELTA;
		case EnConfluence : return CONFLUENCE_H4_DELTA;
		case EnTrac : return TRAC_H4_DELTA;
		default : return TEXTILE_H4_DELTA;
		}
	}

}